<?php

namespace App\Models;

use App\Models\User;

class Score extends Model
{
    const TYPE_OTHER = 0;
    const TYPE_CREATE_TOPIC = 1;
    const TYPE_CREATE_COMMENT = 2;
    const TYPE_CREATE_LIKE = 3;

    protected $casts = [
        'info' => 'json',
    ];

    public static $typeTexts = [
        self::TYPE_OTHER =>['name'=> '其他', 'value' => 1, 'icon' => ''], //一般是特殊临时性质
        self::TYPE_CREATE_TOPIC =>['name'=>  '发表帖子', 'value' => 20, 'icon' => 'iconfont icon-forum1'],
        self::TYPE_CREATE_COMMENT =>['name'=>  '发表回复', 'value' => 10, 'icon' => 'iconfont icon-comment'],
        self::TYPE_CREATE_LIKE =>['name'=> '点赞', 'value' => 5, 'icon' => 'iconfont icon-like'],
    ];

    protected $fillable = ['uid', 'type', 'score', 'total', 'remarks', 'info'];

    protected $hidden = ['info'];

    public static function booted()
    {
        static::created(function ($model) {
            $total = $model->where("uid", $model->uid)->where('id', '<=', $model->id)->sum('score');
            $model->total = $total;
            $model->save();
            User::where('id', $model->uid)->update(['score_count' => $total]);
        });
    }


    public function getTypeTextAttribute()
    {
        return self::$typeTexts[$this->type]['name'] ?? '';
    }

    public function getIconAttribute()
    {
        return self::$typeTexts[$this->type]['icon'] ?? '';
    }

//    public function getRemarksAttribute()
//    {
//        $remarks = $this->getOriginal('remarks');
//        $info = $this->info;
//        if (!empty($info['target_title'])) {
//            $remarks .= " " . $info['target_title'];
//        }
//        return $remarks;
//    }


    public function user()
    {
        return $this->belongsTo(User::class, "uid");
    }

    private static function assign($type, $uid, $score, $remarks, array $info = [])
    {
        return static::create([
            'uid' => $uid,
            'type' => $type,
            'score' => $score ?? self::$typeTexts[$type]['value'] ?? 0,
            'remarks' => $remarks ?? '',
            'info' => $info,
        ]);
    }

    /**
     * 其他类型的积分发送，一般是不固定临时或者活动性质的
     *
     * @param $uid
     * @param $score
     * @param $remarks
     * @param array $info
     * @return mixed
     */
    public static function assignForOther($uid, $score, $remarks, array $info = [])
    {
        return static::assign(self::TYPE_OTHER, $uid, $score, $remarks, $info);
    }

    /**
     * 发布帖子发送
     *
     * @param $uid
     * @param $score
     * @param $remarks
     * @param array $info
     * @return mixed
     *
     */
    public static function assignForCreateTopic($uid, $score = null, $remarks = null, array $info = [])
    {
        return static::assign(self::TYPE_CREATE_TOPIC, $uid, $score, $remarks, $info);
    }

    /**
     * 发布回复发送
     *
     * @param $uid
     * @param $score
     * @param $remarks
     * @param array $info
     * @return mixed
     *
     */
    public static function assignForCreateComment($uid, $score = null, $remarks = null, array $info = [])
    {
        return static::assign(self::TYPE_CREATE_COMMENT, $uid, $score, $remarks, $info);
    }

    /**
     * 进行点赞发送
     *
     * @param $uid
     * @param $score
     * @param $remarks
     * @param array $info
     * @return mixed
     *
     */
    public static function assignForCreateLike($uid, $score = null, $remarks = null, array $info = [])
    {
        return static::assign(self::TYPE_CREATE_LIKE, $uid, $score, $remarks, $info);
    }


}
